<?php
class ModelMessageNews extends Model {
	
	public function addNews($data){
		$this->db->query("insert into `".DB_PREFIX."message_news` set create_time=now(),type='".$data['type']."',`value`='".$data['value']."',name='".$data['name']."'");
		$lastId = $this->db->getLastId();
		if(isset($data['article_route'])){
			foreach ($data['article_route'] as $article_id) {
				$this->db->query('insert into '.DB_PREFIX."message_article_to_news set news_id='".(int)$lastId."' , news_article_id='".(int)$article_id."'");
				//$this->db->query('update '.DB_PREFIX."message_news_article set news_id='".(int)$lastId."' where news_article_id='".(int)$article_id."'");
			}
		}
		
		
	}
	
	public function editNews($news_id, $data) {
		$this->db->query("update  `".DB_PREFIX."message_news`  set create_time=now(),type='".$data['type']."',`value`='".$data['value']."',name='".$data['name']."' where news_id='".(int)$news_id."'");
		if(isset($data['article_route'])){
				$this->db->query('delete from '.DB_PREFIX."message_article_to_news where news_id='".(int)$news_id."'");
			foreach ($data['article_route'] as $article_id) {
				$this->db->query('insert into '.DB_PREFIX."message_article_to_news set news_id='".(int)$news_id."', news_article_id='".(int)$article_id."'");
			}
		}
		
	}
	
	
	public function editNewsToMenu($menu_route_id, $data) {
		$this->db->query("delete from  `".DB_PREFIX."message_news_to_route`  where menu_route_id='".$menu_route_id."'");
		if(isset($data['news_info_route'])){
			foreach ($data['news_info_route'] as $news_id) {
				$this->db->query("insert into  `".DB_PREFIX."message_news_to_route`  set menu_route_id='".$menu_route_id."' , news_id='".(int)$news_id."'");
			}
		}
		
	}
	
	public function getNewsArticle($news_id){
		$sql ="select * from ".DB_PREFIX."message_news_article where news_id='".(int)$news_id."'";
		return $this->db->query($sql)->rows;
	}
	
	
	public function deleteNews($news_id){
		$this->db->query('delete from '.DB_PREFIX."message_news where news_id='".(int)$news_id."'");
	}
	
	public function getNewsArticleByUrl($menu_url){
		$sql ="select mna.* from ".DB_PREFIX."message_news_article mna left join ".DB_PREFIX."config_menu_route cmc on(cmc.menu_route_id=mna.menu_route_id) where cmc.menu_url='".$menu_url."'";
		return $this->db->query($sql)->rows;
	}
	
	
	private function filterSql($data=array()){
		$sql ="";
		if(isset($data['filter_name'])&&!empty($data['filter_name'])){
			$sql .=" and name like '%".$data['filter_name']."%'";
		}
		return $sql;
	}
	
	public function getNewss($data=array()){
		$sql = "select * from `".DB_PREFIX."message_news` where 1=1";
		$sql .=$this->filterSql($data);	
		$sort_data = array(
			'date_added'
		);	
			
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
	
	public function getNews($news_id){
		$query = $this->db->query("SELECT *  FROM `" . DB_PREFIX . "message_news` where news_id='".(int)$news_id."'");
		return $query->row;
	}
	
	public function getTotalNews(){
		$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "message_news`");
		return $query->row['total'];
	}
	
	
	
	
	public function getNewsByRouteId($menu_route_id) {
	    $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "message_news` mn right join ".DB_PREFIX."message_news_to_route mntr on(mn.news_id=mntr.news_id) WHERE mntr.menu_route_id = '" . (int)$menu_route_id . "'");
		$news = $query->row;
		if($news){
			$result = array(
				'create_time'=>$news['create_time'],
				'name'=>$news['name'],
				'type'=>$news['type'],
				'value'=>$news['value'],
				'articles'=>$this->getNewsArticle($news['news_id']),
			);
			return $result;
		}
		return false;
	}
	
	
}
?>